<?			   $year = GetParam("periodYear","");
               $tahun = substr($year,2,4);
               $bulan = "01";
/*
               $select1q = mysql_db_query(DB_NAME,"SELECT distinct skada FROM ais_memorial WHERE ledger = '$tahun$bulan' AND ktrasl = '".GetOfficeID()."' AND LEFT(nobkt,2) <> 'MC' AND LEFT(nobkt,2) <> 'ME' ORDER BY skada");
               */
               $select1q = mysql_db_query(DB_NAME,"SELECT skada FROM tblcoa WHERE ledger = '$year' AND ktrasl = '".GetOfficeID()."' AND tipe=0");
               
               $select1 = mysql_fetch_row($select1q);
               if($select1){
               do{
               //echo "<br>$select1[0]<br>==================<br>";
               $skada = "$select1[0]";

                  // Jumlahkan semua dengan skada yang sama...
                  $select2q = mysql_db_query(DB_NAME,"SELECT * FROM ais_memorial WHERE skada = '$select1[0]' AND ledger = '$tahun$bulan' AND ktrasl = '".GetOfficeID()."' AND LEFT(nobkt,2) <> 'MC' AND LEFT(nobkt,2) <> 'ME'");
                  //print "SELECT * FROM ais_memorial WHERE skada = '$select1[0]' AND ledger = '$tahun$bulan' AND ktrasl = '".GetOfficeID()."' AND LEFT(nobkt,2) <> 'MC' AND LEFT(nobkt,2) <> 'ME'";
                  $select2 = mysql_fetch_row($select2q);
                  if($select2){
                  do{

                     if($select2[23]=="D"){

                        //echo "+ $select2[22]<br>";
                        $debet[$skada] = $debet[$skada] + $select2[25];

                     }elseif($select2[23]=="K"){

                        //echo "- $select2[22]<br>";
                        $credit[$skada] = $credit[$skada] + $select2[25];

                     }

                  }while($select2 = mysql_fetch_row($select2q));
                  }

                  //echo "<br>$debet[$skada] - $credit[$skada]<br><br>";
                  $select3q = mysql_db_query(DB_NAME,"SELECT * FROM tblcoa WHERE skada = '$select1[0]' AND ledger = '$year'");

                  $select3 = mysql_fetch_row($select3q);
                  if($select3){

                     $aku13[$skada] = ($select3[14] + $debet[$skada]) - $credit[$skada];
                     //echo "$select1[0] = $aku1[$skada]<br>";

                     $update1 = mysql_db_query(DB_NAME,"UPDATE tblcoa SET
                                               mutd01 = '$debet[$skada]',
                                               mutk01 = '$credit[$skada]',
                                               aku01 = '$aku13[$skada]' WHERE skada = '$skada'");

                     $update2 = mysql_db_query(DB_NAME,"UPDATE ais_history_ledger SET
                                               status = 'Close'
                                               WHERE month = '$month' AND year = '$year' AND ktrasl = '".GetOfficeID()."'");
                     GetTotal_salrlytd();
                     $SQLUpdate = "UPDATE tblcoa SET aku01 = awal + mutd01 - mutk01";
                     mysql_db_query(DB_NAME,$SQLUpdate);
                  }

               }while($select1 = mysql_fetch_row($select1q));
               }

                     print "
                     <script>
                     alert('Posted and Closed !!!');
                     self.location.replace('$FileName');
                     </script>
                     ";

               
function Get_salrlytd() {
   global $DBConnection;
  $SQL = "SELECT salrlytd FROM tblprofile";
  return $DBConnection->dbc->get_var($SQL);
}

function GetTotal_salrlytd() {
   global $DBConnection;
   $SQL = "SELECT sum(mutd01) as debet,sum(mutk01)  as kredit
           FROM tblcoa
           LEFT JOIN tblcoagroup ON tblcoa.grup=tblcoagroup.group_id
           WHERE coatype = 'REVENUES' OR coatype = 'EXPENSES' OR coatype = 'OTHER EXPENSES'
           ";
  //print $SQL; die();
  $MyResult = $DBConnection->dbc->get_row($SQL);
  $SQL  = "UPDATE tblcoa SET mutd01='".$MyResult->debet."', mutk01='".$MyResult->kredit."' WHERE skada='".Get_salrlytd()."' AND ledger='".GetParam("periodYear","")."'";
}
?>